define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Hongbaodetail, $window, $interval, GoDialog, GoPage, $animate, $animateCss, $stateParams, Auth, $timeout) {
		
		$scope.red_id = $stateParams.id;
		$scope.count = 0;//点击多少次，就可以获得红包
		$scope.timmer = null;//定时器
		
		//页面激活
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.session = Auth.get_user_id();
			if($scope.session == ""){
				GoDialog.alert("提示", "请先登录",function(){
					GoPage.go_states("t-login");
				});
			}else{
				$scope.init_data();
			}
		}, false);
		
		
		//初始化页面，红包雨开始时间，持续时间
		$scope.init_data = function(){
			Hongbaodetail.getDetail($scope.session,$scope.red_id,function(data) {
//				console.log(data)
				if(!data.success){//提示活动已经结束
					GoDialog.alert("提示", data.msg,function(){
						GoPage.go_states("hongbaolist");
					});
					return;
				}
				$scope.data = data.result;
				var duration = parseInt(data.result.duration);//持续时间
				var beginTime = parseInt(data.result.releasetime);//红包开始时间
				var process_time = parseInt(data.process_time);//服务器时间
				var cha = beginTime-process_time;//差多少开始红包雨，单位秒
//				console.log(cha)
				if(process_time>=beginTime+duration){//结束
					$scope.showText = "活动已经结束了！";
				}else if(cha>60*60){//超过1小时
					$scope.showText = "活动还未开始！";
				}else if(cha>0&&cha<=60*60){//1小时之内
					$scope.showText = "倒计时";
					getReleasetime(cha);//马上显示，避免1s后再显示
					var chaTimer = $interval(function(){
						cha--;
						getReleasetime(cha);
						if(cha==0){
							$interval.cancel(chaTimer);//取消定时器
							$scope.showRedBag(duration);//开始红包雨
							$scope.showText = "";
						}
					},1000)
				}else{//开始
					var duration = beginTime+duration - process_time;
//					console.log("开始"+duration)
					if(duration>0){
						$scope.showRedBag(duration);
					}
				}
			})
		}
		
		//计算倒计时，为了显示倒计时
		function getReleasetime(releasetime){
			var min = parseInt(releasetime/60);//分钟
			var seconde = releasetime%60;//秒
			$scope.releasetime = min+":"+seconde;
		}
		
		//红包持续时间结束时发送红包请求
		function getRecord(){
			Hongbaodetail.getRecord($scope.session,$scope.red_id,function(data) {
				if(data.success){
					GoDialog.alert("提示", "恭喜您，获得"+data.result.red_amount+"元红包！",function(){
						GoPage.go_states("hongbaolist");
					});
				}else{
					GoDialog.alert("提示", data.msg ,function(){
						GoPage.go_states("hongbaolist");
					});
				}
			})
		}

		//显示红包，开始红包雨
		$scope.showRedBag = function(duration) {
			$scope.timmer = $interval($scope.moveStart, 300);//数字可以控制红包的密度，数字越大，密度越小
			$scope.showTime = duration;
			$timeout(function(){//获取红包
				$interval.cancel($scope.timmer); //取消定时器--关键
				if($scope.count>0) {
					getRecord();
				}else{
					GoDialog.alert("提示", "很遗憾，一个红包您都没有抓到",function(){
						GoPage.go_states("hongbaolist");
					});
				}
			},duration*1000);
			var showTimmer = $interval(function() {//显示倒计时
				$scope.showTime--;
				if($scope.showTime==0){
					$interval.cancel(showTimmer);
				}
			},1000);
		};
		
		
		//移除可见区域外的红包节点，先不用
		$scope.removepackage = function() {
			$interval(function() {
				var nodejq = angular.element(document.getElementById("baobao"));
				var childs = nodejq.children();
				//子类元素数目
				for(var jj = 0; jj < childs.length / 4; jj++) {
					var node = childs[childs.length - jj];
					if(node != undefined)
						node.remove();
				}
			}, 1000);
		};
		
		//开始红包
		$scope.moveStart = function() {
			var nodejq = angular.element(document.getElementById("baobao"));
			for(var i = 0; i < 2; i++) {//这里也可以控制红包的密度
				var m = parseInt(Math.random() * 700 + 100);
				var j2 = parseInt(Math.random() * 300 + 1200);
				var j = parseInt(Math.random() * 1600);
				var j1 = parseInt(Math.random() * 300 + 300);
				var n = parseInt(Math.random() * 10 + (-10));
				//加入节点
				var node = angular.element('<img src="./img/guilin/redrain/icon_hb@3x.png" ></img>');
				node.bind("click", function() {//点击红包
					var item = angular.element(this);
					item.attr("src", "./img/guilin/redrain/icon_hb_click@2x.png");
					$scope.count++;//点中一次，加+1
//					if($scope.count == 5) {
//						var nodeshow = angular.element(document.getElementById("baobaoshow"));
//						nodeshow.attr("style", "display:inherit;");
//						$interval.cancel($scope.timmer); //取消定时器--关键
//						nodejq.removeClass("bg_1");
//						setTimeout(function() {
//							nodejq.addClass("bg_2");
//						}, 1000);//多少秒显示停止画面，一般和$animateCss里的duration相同
//					}
				});
				nodejq.prepend(node);
				//动画
				var animate = $animateCss(node, {
					from: { 'left': j + "px", 'top': n + "px" },
					to: { 'left': (j - j1) + "px", 'top': ($window.innerHeight + 20) + "px" },
					duration: 4//控制红包的速度，数字越大，速度越慢
				});
				animate.start();
			}
		};
		
		
	}

	controller.$inject = ['$scope', 'Hongbaodetail', '$window', '$interval', 'GoDialog', 'GoPage', '$animate', '$animateCss', '$stateParams','Auth','$timeout'];

	return app.registerController("hongbaodetailctrl", controller);
});